<?php
namespace app\warning\library;


/***
 * 评估类
 *
 */
class Evaluation {
	
	/**
	 * 自动评估C1(原料危险性)
	 * @param string $enterpriseId 企业ID
	 * @return int $score 分数
	 */
	public function  c1($enterpriseId){
		$model = model('basic/chemical');
		$count = count($model->where(array('is_supervised'=>1,'enterpriseId'=>$enterpriseId))->select());
		if($count){
			$num1 = count($model->where(array('is_supervised'=>1,'enterpriseId'=>$enterpriseId,'is_major'=>1))->select());
			if($num1>0){
				return $score = 100;
			}else{
				$num2 = count($model->where(array('is_supervised'=>1,'enterpriseId'=>$enterpriseId,'is_major'=>0))->select());
				if($num2>0){
					return $score = 50;
				}
			}
		}else{
			return $score = 0;
		}
	}
	
	/*
	 * 自动评估C2(原料存储量或最大使用量)
	 * @param string $enterpriseId 企业ID
	 * @return int $score 分数
	 */
	public  function  c2($enterpriseId){
		$res = collection(model('basic/danger')->with('chemicals')->where(array('enterpriseId'=>$enterpriseId,'sourceType'=>2))->select())->toArray();
		$arr = [];
		if($res){
			foreach($res[0]['chemicals'] as $key=>$value){
		
				$arr[$key] = $value['pivot']["stock"]/$value['limit'];
		
			}
			$Q = array_sum($arr);
		
			switch($Q){
				case $Q>=1:
					return $score = 100;
					break;
		
				case 0.1<=$Q&&$Q<1:
					return $score = 50;
					break;
		
				case 0<=$Q&&$Q<0.1:
					return $score = 0;
					break;
			}
		}else{
			return $score = 0;
		}
	}
	
	/**
	 * 自动评估C3(原料存储量或最大使用量)
	 * @param string $enterpriseId 企业ID
	 * @return int $score 分数
	 */
	public  function  c3($enterpriseId){
		$count = count(model('basic/craft')->where(array('enterpriseId'=>$enterpriseId,'sourceType'=>3))->where('typeId','between','1,18')->select());
	
		if($count){
			$score = $count*50;
			$score <= 100?$score:100;
			return $score;
		}else{
			$num = count(model('basic/craft')->where(array('enterpriseId'=>$enterpriseId,'sourceType'=>3,'is_danger'=>1))->select());
			if($num){
				$score = 25*$num;
				$score <= 100?$score:100;
				return $score;
			}else{
				return $score = 0;
			}
	
		}
	}
	
	
	/**
	 * 自动评价C15(承包商管理)
	 * @param string $enterpriseId 企业ID
	 * @return int $scoref 分数
	 */
	public function  c15($enterpriseId){
		$major = model('basic/staff')->where(array('enterpriseId'=>$enterpriseId,'post'=>2))->where('securityCertificate','<>',0)->find();
	
		if($major){
			$manage = model('basic/staff')->where(array('enterpriseId'=>$enterpriseId))->where('post','in','2,3,6,7,8,9,10,18,11,13,15')->where('securityCertificate','eq',0)->select();
			if($manage){
				return $score = 50;
			}else{
				return $score = 0;
			}
		}else{
			return $score = 100;
		}
	
	}
	/**
	 * 自动评估C18(应急管理)
	 * @param string $enterpriseId
	 * @return int $score 分数
	 */
	public  function  c18($enterpriseId){
		$count = count(model('plan/plan')->where(array('unitId'=>$enterpriseId,'formId'=>1))->select());
		$score = 100;
		if($count){
			//是否定期进行预案演练
			$id = model('rehearse/simulation')->where(array('eventType'=>2,'enterpriseId'=>$enterpriseId))->column('id','id');
			$num = count($id);
			if($num){
				$flag = null;
				foreach ($id as $value){
					$evaluation = model('rehearse/record')->where(array('eventId'=>$value))->value('evaluate');
					if($evaluation){
						continue;
					}else{
						$flag=1;
					}
				}
				if($flag){
					return $score = 50;
				}else{
					return $score = 0;
				}
			}else{
				return $score = 50;
			}
		}else{
			return $score = 100;
		}
	}
}
